package com.changwu.cwbase.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.StringUtils;
import com.changwu.cwbase.R;
import com.changwu.cwbase.base.BasicDialog;
import com.changwu.cwbase.interfaces.AppEditorListener;

import java.lang.ref.WeakReference;


/**
 * @date:2017/2/16
 * @author: 魏寿明
 * @description:
 */

public class CWDialog extends BasicDialog implements View.OnClickListener {
    private View view;
    private WeakReference<DialogInterface> mDialog;
    public DialogInterface.OnClickListener mPositiveListener;
    public DialogInterface.OnClickListener mNegativeListener;

    // dialog监听控件
    public CWDialog(Context context, Builder builder) {
        super(context);
        mDialog = new WeakReference<>((DialogInterface) this);
        if (builder.mViewLayoutResId != 0) {
            view = LayoutInflater.from(context).inflate(builder.mViewLayoutResId, null, false);
            setContentView(view);
        } else if (builder.mView != null) {
            setContentView(builder.mView);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.dialog_common, null, false);
            setContentView(view);
        }
        getWindow().setLayout(ScreenUtils.getScreenWidth() * 19 / 20, ViewGroup.LayoutParams.WRAP_CONTENT);
//        getWindow().setGravity(Gravity.BOTTOM);
        this.context = context;
        this.mPositiveListener = builder.mPositiveButtonListener;
        this.mNegativeListener = builder.mNegativeButtonListener;
        init();
        if (!TextUtils.isEmpty(builder.mTitle)) {
            title.setText(builder.mTitle);
        }
        if (!TextUtils.isEmpty(builder.mContent)) {
            content.setText(builder.mContent);
        }
        if (builder.mContentGravity != Gravity.CENTER) {
            content.setGravity(builder.mContentGravity);
        }
        if (!TextUtils.isEmpty(builder.mPositiveButtonText)) {
            submit.setText(builder.mPositiveButtonText);
        }
        if (builder.mPositiveButtonIsVisible) {
            submit.setVisibility(View.VISIBLE);
        } else {
            submit.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(builder.mNegativeButtonText)) {
            cancel.setText(builder.mNegativeButtonText);
        }
        if (builder.mNegativeButtonIsVisible) {
            cancel.setVisibility(View.VISIBLE);
        } else {
            cancel.setVisibility(View.GONE);
        }

    }

    private void init() {
        title = findViewById(R.id.title);
        content = findViewById(R.id.content);
        submit = findViewById(R.id.submit);
        cancel = findViewById(R.id.cancel);
        if (submit != null) {
            submit.setOnClickListener(this);
        }
        if (cancel != null) {
            cancel.setOnClickListener(this);
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit:
                if (mPositiveListener != null) {
                    mPositiveListener.onClick(mDialog.get(), 1);
                } else {
                    dismiss();
                }
                break;
            case R.id.cancel:
                if (mNegativeListener != null) {
                    mNegativeListener.onClick(mDialog.get(), 1);
                }
                dismiss();
                break;
        }
    }

    public static final class Builder {
        public final Context mContext;
        public CharSequence mPositiveButtonText;
        public DialogInterface.OnClickListener mPositiveButtonListener;
        public boolean mPositiveButtonIsVisible;
        public CharSequence mNegativeButtonText;
        public DialogInterface.OnClickListener mNegativeButtonListener;
        public boolean mNegativeButtonIsVisible;
        public CharSequence mTitle;
        public CharSequence mContent;
        public int mContentGravity;
        public int mViewLayoutResId;
        public View mView;
        public boolean mCancelable;
        public DialogInterface.OnCancelListener mOnCancelListener;
        public DialogInterface.OnDismissListener mOnDismissListener;
        public DialogInterface.OnKeyListener mOnKeyListener;

        public Builder(Context context) {
            mContext = context;
            mContentGravity = Gravity.CENTER;
            mNegativeButtonIsVisible = true;
            mPositiveButtonIsVisible = true;
            mCancelable = true;
        }

        public Builder setTitle(@StringRes int titleId) {
            mTitle = mContext.getText(titleId);
            return this;
        }

        public Builder setTitle(@Nullable CharSequence title) {
            mTitle = title;
            return this;
        }

        public Builder setContent(@StringRes int contentId) {
            mContent = mContext.getText(contentId);
            return this;
        }

        public Builder setContent(@Nullable CharSequence content) {
            mContent = content;
            return this;
        }

        public Builder setContentGravity(@Nullable int gravity) {
            mContentGravity = gravity;
            return this;
        }

        public Builder setPositiveButton(@StringRes int textId, final DialogInterface.OnClickListener listener) {
            mPositiveButtonText = mContext.getText(textId);
            mPositiveButtonListener = listener;
            return this;
        }

        public Builder setPositiveButton(CharSequence text, final DialogInterface.OnClickListener listener) {
            mPositiveButtonText = text;
            mPositiveButtonListener = listener;
            return this;
        }

        public Builder setPositiveButtonVisible(boolean isVisible) {
            mPositiveButtonIsVisible = isVisible;
            return this;
        }

        public Builder setNegativeButton(@StringRes int textId, final DialogInterface.OnClickListener listener) {
            mNegativeButtonText = mContext.getText(textId);
            mNegativeButtonListener = listener;
            return this;
        }

        public Builder setNegativeButton(CharSequence text, final DialogInterface.OnClickListener listener) {
            mNegativeButtonText = text;
            mNegativeButtonListener = listener;
            return this;
        }

        public Builder setNegativeButtonVisible(boolean isVisible) {
            mNegativeButtonIsVisible = isVisible;
            return this;
        }

        public Builder setPositiveListener(final DialogInterface.OnClickListener listener) {
            mPositiveButtonListener = listener;
            return this;
        }

        public Builder setNegativeListener(final DialogInterface.OnClickListener listener) {
            mNegativeButtonListener = listener;
            return this;
        }

        public Builder setView(int layoutResId) {
            mViewLayoutResId = layoutResId;
            return this;
        }

        public Builder setView(View view) {
            mView = view;
            return this;
        }

        public CWDialog create() {
            // We can't use Dialog's 3-arg constructor with the createThemeContextWrapper param,
            // so we always have to re-set the theme
            final CWDialog dialog = new CWDialog(mContext, this);

            dialog.setCancelable(mCancelable);
            if (mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            dialog.setOnCancelListener(mOnCancelListener);
            dialog.setOnDismissListener(mOnDismissListener);
            if (mOnKeyListener != null) {
                dialog.setOnKeyListener(mOnKeyListener);
            }
            return dialog;
        }

        public CWDialog show() {
            final CWDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }

    public static void showAlertDialog(Context context, String title, String content) {
        CWDialog.Builder builder = new CWDialog.Builder(context)
                .setTitle(title)
                .setContent(content)
                .setNegativeButtonVisible(false);
        builder.show();
    }

    public static void showCWDialog(Context context, String title, String content) {
        showCWDialog(context, title, content, null);
    }

    public static void showCWDialog(Context context, String title, String content, final DialogInterface.OnClickListener listener) {
        CWDialog.Builder builder = new CWDialog.Builder(context)
                .setTitle(title)
                .setContent(content)
                .setPositiveListener(listener);
        builder.show();
    }

    public static void showCustomeDialog(Context context, String title, String content, final DialogInterface.OnClickListener listener) {
        final View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_choose_one, null, false);
        CWDialog.Builder builder = new CWDialog.Builder(context)
                .setView(dialogView);
        builder.show();
    }

    public static void showEditorDialog(Context context, TextView tv, String content, final AppEditorListener listener) {
        showEditorDialog(context, tv, content, InputType.TYPE_CLASS_TEXT, listener);
    }

    public static void showEditorDialog(Context context, TextView tv, String content, int type, final AppEditorListener listener) {
        final View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_input_content, null, false);
        final EditText etName = dialogView.findViewById(R.id.content);
        if (!StringUtils.isEmpty(content)) {
            etName.setText(content);
        }
        etName.setInputType(type);
        CWDialog.Builder builder = new CWDialog.Builder(context)
                .setView(dialogView)
                .setTitle("提示")
                .setPositiveListener(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        listener.onClick(dialogInterface, etName.getText().toString());
                    }
                });
        builder.show();
    }
}
